package com.example.controller;

import com.example.VO.BaseResultVO;
import com.example.VO.LoginResultVO;
import com.example.VO.UserVO;
import com.example.mapper.RequestMapper;
import com.example.model.User;
import com.example.service.RequestService;
import com.example.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.*;

/**
 * Created by ykbboom on 2017/2/13.
 */
@RestController
@EnableAutoConfiguration
@PropertySource(value = "classpath:/custom_properties/db.properties", ignoreResourceNotFound = true)
@RequestMapping(value = "/easygo/user/")
public class UserController extends BaseController {
    private Logger logger = Logger.getLogger(UserController.class);
    @Value("${CONSTANT_USER}")
    private String accountName;
    @Value("${CONSTANT_PASSWORD}")
    private String password;

    @Autowired
    private UserService userService;
    @Autowired
    private RequestMapper requestMapper;
    /**
     * 测试接口类
     * */
    @RequestMapping(value = "hello",method = RequestMethod.GET)
    public int hello(){
        logger.info("运行hello()方法");
        int count = requestMapper.getUserRequestCount(9);
        return count;


    }

    /**
     * 处理用户注册
     * */
    @RequestMapping(value = "register",method = RequestMethod.POST)
    public BaseResultVO userRegister(@RequestParam String userName,
                                     @RequestParam String userPass,
                                     @RequestParam String userPhone){
        logger.info("运行userRegister()方法/"+userName+"/"+userPass+"/"+userPhone);
        if (userService.checkUserExists(userPhone)){
            return new BaseResultVO(null,BaseResultVO.RESULT_CODE_FAIL,"此手机号已注册");
        }else{
            userService.addUser(new User(userName,userPhone,userPass));
            return new BaseResultVO(null,BaseResultVO.RESULT_CODE_OK,null);
        }

    }
    /**
     * 用户登录
     * */
    @RequestMapping(value = "login",method = RequestMethod.POST)
     public LoginResultVO userLogin(@RequestParam String userPhone,
                                    @RequestParam String userPass){
        logger.info("运行userLogin()方法/"+"phone:"+userPhone+"/"+"password:"+userPass);
        User user = userService.validateUser(userPhone,userPass);
        LoginResultVO Vo = new LoginResultVO();
        if (user!=null){
            logger.info(user.toString());
                Vo.setResultCode(BaseResultVO.RESULT_CODE_OK);
                Vo.setMsg(null);
                Vo.setUserName(user.getUserName());
                Vo.setUserPoint(user.getUserPoint());
                return Vo;
        }
        Vo.setResultCode(BaseResultVO.RESULT_CODE_FAIL);
        Vo.setMsg("账号或密码错误");
        Vo.setUserName(null);
        Vo.setUserPoint(0);
        return Vo;
     }
    /**
     * 查看用户积分
     * */
    @RequestMapping(value = "checkUserPoint",method = RequestMethod.POST)
    public UserVO checkUserPoint(@RequestParam String userPhone){
        logger.info("运行checkUserPoint()方法/"+"phone:"+userPhone+"/");
        UserVO vo = userService.checkUserPoint(userPhone);
        return vo;
    }
    /**
     * 更新用户密码
     * */
    @RequestMapping(value = "updatePassword",method = RequestMethod.POST)
    public BaseResultVO updateUserPassword(@RequestParam String userPhone,@RequestParam String newPass){
        return userService.updateUserPass(userPhone,newPass);
    }
}
